
package net.core.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * @author linfp
 * 
 */
public class MiniUtil {
	/*
	 * 字符串转为Map类型。字符串为a:1, b:2, c:3 返回Map后的信息为 map<a, 1><b, 2><c, 3>
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, String> strToMap(String str) {
		if (null == str)
			return new HashMap();
		;
		str = str.replaceAll("'", "");
		HashMap<String, String> perMap = new HashMap<String, String>();
		String[] _str = str.split(",");
		for (int i = 0; i < _str.length; i++) {
			if (_str[i].trim().indexOf(":") != -1) {
				String[] xtr = _str[i].trim().split(":");
				if (null != xtr[0] && xtr.length >= 2) {
					if (xtr[0].indexOf(".") == -1) {
						// System.out.println(xtr[0].trim() + " : "
						// +xtr[1].trim());
						perMap.put(xtr[0].trim(), xtr[1].trim());
					} else {
						// System.out.println("key : "+
						// xtr[0].substring(xtr[0].indexOf(".")+1,
						// xtr[0].length()).trim());
						perMap.put(xtr[0].substring(xtr[0].indexOf(".") + 1, xtr[0].length())
								.trim(), xtr[1].trim());
					}
				}
			}
		}
		return perMap;
	}

	/*
	 * 字符串转为Map类型。字符串为a:1, b:2, c:3 返回Map后的信息为 map<a, 1><b, 2><c, 3>
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> strToMapObject(String str) {
		if (null == str)
			return new HashMap();
		;
		str = str.replaceAll("'", "");
		HashMap<String, Object> perMap = new HashMap<String, Object>();
		String[] _str = str.split(",");
		for (int i = 0; i < _str.length; i++) {
			if (_str[i].trim().indexOf(":") != -1) {
				String[] xtr = _str[i].trim().split(":");
				if (null != xtr[0] && xtr.length >= 2) {
					if (xtr[0].indexOf(".") == -1) {
						// System.out.println(xtr[0].trim() + " : "
						// +xtr[1].trim());
						perMap.put(xtr[0].trim(), xtr[1].trim());
					} else {
						// System.out.println("key : "+
						// xtr[0].substring(xtr[0].indexOf(".")+1,
						// xtr[0].length()).trim());
						perMap.put(xtr[0].substring(xtr[0].indexOf(".") + 1, xtr[0].length())
								.trim(), xtr[1].trim());
					}
				}
			}
		}
		return perMap;
	}

	@SuppressWarnings("deprecation")
	public static String getDir(HttpServletRequest request, String docPath) {
		String realPath = request.getRealPath(docPath);
		File dir = new File(realPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		return realPath;
	}

	public static void seeXml(HttpServletResponse repon, String pahtUrl, String fileName) {
		repon.setContentType("application/vnd.ms-excel");
		String fname = "";
		try {
			fname = java.net.URLEncoder.encode(fileName, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
		}// 转码
		repon.setHeader("Content-disposition", "attachment; filename=" + fname + ".xls");// 设置网页为下载
		try {
			FileInputStream in = new FileInputStream(pahtUrl);
			byte[] buffer = new byte[in.available()];
			in.read(buffer);
			repon.getOutputStream().write(buffer);
			repon.getOutputStream().flush();
			in.close();
		} catch (Exception e) {
		}
	}

	// 转换HashMap参数值。邮encode转decode
	@SuppressWarnings("unchecked")
	public static HashMap<String, String> decode(HashMap<String, String> perMap) {
		Iterator it = perMap.keySet().iterator();
		HashMap<String, String> _hashMap = new HashMap<String, String>();
		String itkey = "";
		while (it.hasNext()) {
			itkey = (String) it.next();
			try {
				String obj = java.net.URLDecoder.decode(perMap.get(itkey), "utf-8");
				obj = obj.replaceAll("%", " ");
				_hashMap.put(itkey, obj);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return _hashMap;
	}

	public static String decode(String str) {

		try {
			str = java.net.URLDecoder.decode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static List<Map<String, String>> jsonToList(String jsonStr) {

		JSONArray jsonArray = JSONArray.fromObject(jsonStr);

		net.sf.json.JSONObject jsonObject;

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		for (int i = 0; i < jsonArray.size(); i++) {

			jsonObject = jsonArray.getJSONObject(i);
			Iterator<?> keyIter = jsonObject.keys();

			Map<String, String> valueMap = new HashMap<String, String>();
			while (keyIter.hasNext()) {
				String key = keyIter.next().toString();
				String value = MiniUtil.decode(jsonObject.get(key).toString());
				valueMap.put(key, value);
			}
			;
			list.add(valueMap);
		}
		return list;
	}
}
